package cn.falconnect.adserver.adapter.persistence.dao;

import java.util.List;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.One;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.mapping.FetchType;
import org.springframework.stereotype.Component;

import cn.falconnect.adserver.adapter.persistence.typehandler.AdFormatEnumTypeHandler;
import cn.falconnect.adserver.domain.ad.Strategy;

@Component
@Mapper
public interface StrategyDao {

	@Results(value = { @Result(column = "campaign_id", property = "campaign.id"),
			@Result(column = "start_time", property = "startTime"), @Result(column = "end_time", property = "endTime"),
			@Result(column = "max_impressions", property = "maxImpressions"),
			@Result(column = "day_impressions", property = "dayImpressions"),
			@Result(column = "max_clicks", property = "maxClicks"),
			@Result(column = "day_clicks", property = "dayClicks"),
			@Result(column = "day_budget", property = "dayBudget"),
			@Result(column = "speed_mode", property = "speedMode"),
			@Result(column = "speed_base", property = "speedBase"),
			@Result(column = "create_time", property = "createTime"),
			@Result(column = "update_time", property = "updateTime"),
			@Result(column = "buy_mode", property = "buyMode"), @Result(column = "unit_price", property = "unitPrice"),
			@Result(column = "fee_type", property = "feeType"),@Result(column = "strategy_group_id",property ="strategyGroupId") })
	@Select("select * from strategy where id = #{id}")
	Strategy findById(@Param("id") long id);

	@Results(value = { @Result(column = "campaign_id", property = "campaign.id"),
			@Result(column = "start_time", property = "startTime"), @Result(column = "end_time", property = "endTime"),
			@Result(column = "max_impressions", property = "maxImpressions"),
			@Result(column = "day_impressions", property = "dayImpressions"),
			@Result(column = "max_clicks", property = "maxClicks"),
			@Result(column = "day_clicks", property = "dayClicks"),
			@Result(column = "day_budget", property = "dayBudget"),
			@Result(column = "speed_mode", property = "speedMode"),
			@Result(column = "speed_base", property = "speedBase"),
			@Result(column = "create_time", property = "createTime"),
			@Result(column = "update_time", property = "updateTime"),
			@Result(column = "buy_mode", property = "buyMode"), @Result(column = "unit_price", property = "unitPrice"),
			@Result(column = "fee_type", property = "feeType"),
			@Result(column = "strategy_group_id",property ="strategyGroupId"),
			@Result(column = "campaign_id",property ="campaign",one = @One(select = "cn.falconnect.adserver.adapter.persistence.dao.CampaignDao.findAvailableById" ,fetchType = FetchType.DEFAULT))
	})
	@Select("select * from strategy where id = #{id} and status = #{status}")
	Strategy selectById(@Param("id") long id, @Param("status") int status);

	@Results(value = {@Result(column = "id", property = "id", id = true),
			@Result(column = "campaign_id", property = "campaignId"),
			@Result(column = "start_time", property = "startTime"),
			@Result(column = "end_time", property = "endTime"),
			@Result(column = "max_impressions", property = "maxImpressions"),
			@Result(column = "day_impressions", property = "dayImpressions"),
			@Result(column = "max_clicks", property = "maxClicks"),
			@Result(column = "day_clicks", property = "dayClicks"),
			@Result(column = "day_budget", property = "dayBudget"),
			@Result(column = "speed_mode", property = "speedMode"),
			@Result(column = "speed_base", property = "speedBase"),
			@Result(column = "format_type", property = "formatTypeList",typeHandler=AdFormatEnumTypeHandler.class),
			@Result(column = "create_time", property = "createTime"),
			@Result(column = "update_time", property = "updateTime"),
			@Result(column = "buy_mode", property = "buyMode"), @Result(column = "unit_price", property = "unitPrice"),
			@Result(column = "fee_type", property = "feeType"),
			@Result(column = "strategy_group_id",property ="strategyGroupId"),
			@Result(column = "campaign_id",property ="campaign",one = @One(select = "cn.falconnect.adserver.adapter.persistence.dao.CampaignDao.findAvailableById" ,fetchType = FetchType.DEFAULT)),
			@Result(column = "strategy_group_id",property ="strategyGroup",one = @One(select = "cn.falconnect.adserver.adapter.persistence.dao.StrategyGroupDao.selectById" ,fetchType = FetchType.DEFAULT)),
			@Result(column = "id",property ="specifyCreativeIds",one = @One(select = "findCreativeIds" ,fetchType = FetchType.DEFAULT))
	})
	@Select("select * from strategy")
	List<Strategy> findAll();
	
	/**
	 * 通过strategyId获取创意id列表。
	 * @param strategyId
	 * @return
	 */
	@Select("select creative_id from strategy_creative_map where strategy_id = #{value} and status = 1")
	List<Long> findCreativeIds(long strategyId);
	
	/**
	 * 没有关联策略组
	 * @return
	 */
	@Results(value = {@Result(column = "id", property = "id", id = true),
			@Result(column = "campaign_id", property = "campaignId"),
			@Result(column = "start_time", property = "startTime"),
			@Result(column = "end_time", property = "endTime"),
			@Result(column = "max_impressions", property = "maxImpressions"),
			@Result(column = "day_impressions", property = "dayImpressions"),
			@Result(column = "max_clicks", property = "maxClicks"),
			@Result(column = "day_clicks", property = "dayClicks"),
			@Result(column = "day_budget", property = "dayBudget"),
			@Result(column = "speed_mode", property = "speedMode"),
			@Result(column = "speed_base", property = "speedBase"),
			@Result(column = "format_type", property = "formatTypeList",typeHandler=AdFormatEnumTypeHandler.class),
			@Result(column = "create_time", property = "createTime"),
			@Result(column = "update_time", property = "updateTime"),
			@Result(column = "buy_mode", property = "buyMode"), @Result(column = "unit_price", property = "unitPrice"),
			@Result(column = "fee_type", property = "feeType"),
			@Result(column = "strategy_group_id",property ="strategyGroupId"),
			@Result(column = "campaign_id",property ="campaign",one = @One(select = "cn.falconnect.adserver.adapter.persistence.dao.CampaignDao.findAvailableById" ,fetchType = FetchType.DEFAULT))
	})
	@Select("select * from strategy")
	List<Strategy> findAll2();

	@Results(value = { @Result(column = "campaign_id", property = "campaign.id"),
			@Result(column = "start_time", property = "startTime"), @Result(column = "end_time", property = "endTime"),
			@Result(column = "max_impressions", property = "maxImpressions"),
			@Result(column = "day_impressions", property = "dayImpressions"),
			@Result(column = "max_clicks", property = "maxClicks"),
			@Result(column = "day_clicks", property = "dayClicks"),
			@Result(column = "day_budget", property = "dayBudget"),
			@Result(column = "speed_mode", property = "speedMode"),
			@Result(column = "speed_base", property = "speedBase"),
			@Result(column = "format_type", property = "formatTypeList",typeHandler=AdFormatEnumTypeHandler.class),
			@Result(column = "create_time", property = "createTime"),
			@Result(column = "update_time", property = "updateTime"),
			@Result(column = "buy_mode", property = "buyMode"), @Result(column = "unit_price", property = "unitPrice"),
			@Result(column = "fee_type", property = "feeType"),
			@Result(column = "strategy_group_id",property ="strategyGroupId"),
			@Result(column = "campaign_id",property ="campaign",one = @One(select = "cn.falconnect.adserver.adapter.persistence.dao.CampaignDao.findAvailableById" ,fetchType = FetchType.DEFAULT)),
			@Result(column = "strategy_group_id",property ="strategyGroup",one = @One(select = "cn.falconnect.adserver.adapter.persistence.dao.StrategyGroupDao.selectById" ,fetchType = FetchType.DEFAULT))
	})
	@Select("select * from strategy where update_time >= #{value}")
	List<Strategy> findAfterTime(String warmUpTime);

	@Select("select city_id from strategy_city_map where strategy_id = #{value}")
	List<Integer> findCityIds(long sid);

	@Select("select connect_type from strategy_connect_types where strategy_id = #{value}")
	List<Integer> findConnectTypes(long sid);

	@Select("select connect_type from strategy_connect_types where strategy_id = #{id} and status = #{status}")
	List<Integer> selectConnectTypes(@Param("id") long id, @Param("status") int status);

	@Select("select operator_id from strategy_operator where strategy_id = #{value}")
	List<Integer> findOperator(long sid);

	@Select("select operator_id from strategy_operator where strategy_id = #{id} and status = #{status}")
	List<Integer> selectOperator(@Param("id") long id, @Param("status") int status);

	@Select("select os from strategy_os_map where strategy_id = #{value}")
	List<Integer> findOs(long sid);

	@Select("select os from strategy_os_map where strategy_id = #{id} and status = #{status}")
	List<Integer> selectOs(@Param("id") long id, @Param("status") int status);

	@Results(value = { @Result(column = "campaign_id", property = "campaign.id"),
			@Result(column = "start_time", property = "startTime"), @Result(column = "end_time", property = "endTime"),
			@Result(column = "max_impressions", property = "maxImpressions"),
			@Result(column = "day_impressions", property = "dayImpressions"),
			@Result(column = "max_clicks", property = "maxClicks"),
			@Result(column = "day_clicks", property = "dayClicks"),
			@Result(column = "day_budget", property = "dayBudget"),
			@Result(column = "speed_mode", property = "speedMode"),
			@Result(column = "speed_base", property = "speedBase"),
			@Result(column = "create_time", property = "createTime"),
			@Result(column = "update_time", property = "updateTime"),
			@Result(column = "buy_mode", property = "buyMode"), @Result(column = "unit_price", property = "unitPrice"),
			@Result(column = "fee_type", property = "feeType") })
	@Select("select * from strategy where space_id = #{value}")
	List<Strategy> findByAdSpaceIdFrmDB(long id);

	@Select("select strategy_id from strategy_space_map where space_id = #{value}")
	List<Integer> findSpaceId(long sid);

	@Select("select strategy_id from strategy_space_map where space_id = #{id} and status = #{status}")
	List<Integer> selectSpaceId(@Param("id") long id, @Param("status") int status);

	@Select("select creative_id from strategy_creative_map where strategy_id = #{value}")
	List<Integer> findCreativeId(long sid);

	@Select("select creative_id from strategy_creative_map where strategy_id = #{id} and status = #{status}")
	List<Integer> selectCreativeId(@Param("id") long id, @Param("status") int status);

	@Results(value = { @Result(column = "campaign_id", property = "campaign.id"),
			@Result(column = "start_time", property = "startTime"), @Result(column = "end_time", property = "endTime"),
			@Result(column = "max_impressions", property = "maxImpressions"),
			@Result(column = "day_impressions", property = "dayImpressions"),
			@Result(column = "max_clicks", property = "maxClicks"),
			@Result(column = "day_clicks", property = "dayClicks"),
			@Result(column = "day_budget", property = "dayBudget"),
			@Result(column = "speed_mode", property = "speedMode"),
			@Result(column = "speed_base", property = "speedBase"),
			@Result(column = "create_time", property = "createTime"),
			@Result(column = "update_time", property = "updateTime"),
			@Result(column = "buy_mode", property = "buyMode"), @Result(column = "unit_price", property = "unitPrice"),
			@Result(column = "fee_type", property = "feeType") })
	@Select("select * from strategy where status = 1")
	List<Strategy> findAvailable();

}
